/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.lps.client;

import com.lps.action.Action;
import com.lps.interfaces.LoanReportService;
import com.lps.model.User;
import com.lps.response.Response;
import java.rmi.Naming;
import java.rmi.RemoteException;
import javax.swing.JOptionPane;

/**
 *
 * @author thetrungbk
 */
public class Client {
    // <editor-fold defaultstate="collapsed" desc="Instance">

    private static Client instance;

    public static Client getInstance() {
        return instance == null ? instance = new Client() : instance;
    }
    // </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="Current User">
    private User currentUser;

    public User getCurrentUser() {
        return currentUser;
    }

    public void setCurrentUser(User currentUser) {
        this.currentUser = currentUser;
    }
    // </editor-fold>
    private String sessionId;

    public String getSessionId() {
        return sessionId;
    }

    public void setSessionId(String sessionId) {
        this.sessionId = sessionId;
    }
    private LoanReportService loanReportService;

    public Client() {
        init();
    }

    public void init() {
        try {
            loanReportService = (LoanReportService) Naming.lookup("rmi://127.0.0.1:1099/bugreport");
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage(), "Error " + ex, JOptionPane.ERROR_MESSAGE);
        }
    }

    /**
     * replace by method: sendAction
     * @return BugReportService
     */
    @Deprecated()
    public LoanReportService getLoanReportService() {
        return loanReportService;
    }

    /**
     *Change-New: Return ManagerRespone
     *Old : Return void
     */
    public Response sendAction(Action action) throws RemoteException {
        if (sessionId != null) {
            action.setSessionId(sessionId);
        }
        return loanReportService.doAction(action);
    }

    public Response sendActionIgnoreException(Action action) {
        if (sessionId != null) {
            action.setSessionId(sessionId);
        }
        Response response;
        try {
            response = loanReportService.doAction(action);
        } catch (Exception ex) {
            response = new Response();
            response.setCode(Response.FAIL);
            response.setMessage(ex.getMessage());
        }
        return response;
    }
}
